package com.nhjf.service.impl.postJob;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nhjf.command.comment.OperationCommand;
import com.nhjf.dao.jobDetails.JobDetailsDao;
import com.nhjf.dao.postJob.PostJobDao;
import com.nhjf.dao.teachers.CoursesTypeDao;
import com.nhjf.dao.teachers.OrganizationHasStudentDao;
import com.nhjf.dao.teachers.TeachersDao;
import com.nhjf.model.jobDetails.JobDetails;
import com.nhjf.model.postJob.PostJob;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.Organization;
import com.nhjf.model.teachers.CoursesType;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.dto.result.ServiceResultDto;
import com.nhjf.pojo.qo.postJob.PostJobQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.teachers.CoursesTypeQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.impl.BaseServiceImpl;
import com.nhjf.service.postJob.PostJobService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
@Transactional
@Service("postJobService")
public class PostJobServiceImpl extends BaseServiceImpl<PostJob, PostJobQo, PostJobDao> implements PostJobService{
	@Autowired
	private PostJobDao postJobDao;
	@Autowired
	private JobDetailsDao jobDetailsDao;
	@Autowired
	private TeachersDao teacherDao;
	@Autowired
	private OrganizationHasStudentDao ostService;
	@Autowired
	private CoursesTypeDao coursesTypeDao;
	@Override
	protected PostJobDao getDao() {
		// TODO Auto-generated method stub
		return postJobDao;
	}
	@Override
	public ServiceResultDto createCommand(OperationCommand command) {
		// TODO Auto-generated method stub
		ServiceResultDto resultDto = new ServiceResultDto();
		PostJob job=new PostJob();

		if(command==null) {
			resultDto.setSuccess(false);
			resultDto.setResultMessage("非法操作");
			return resultDto;
		}
		if(command.getCreatUser()!=null) {
			Teacher teacher= new Teacher();
			teacher.setId(command.getCreatUser());
			job.setCreatUser(teacher);
		}
		if(command.getSetOrganizationId()!=null) {
			Organization organization=new Organization();
			organization.setId(command.getSetOrganizationId());
			job.setOrganizationId(organization);
		}
		if(command.getEndDate()!=null) {
			job.setEndTime(command.getEndDate());
		}
		if(command.getStartDate()!=null) {
			job.setStartTime(command.getStartDate());
		}
		job.setCreatTime(new Date());
		if(StringUtils.isNotBlank(command.getName())) {
			job.setName(command.getName());
		}
		if(command.getTypeOne()!=null) {
			CoursesTypeQo coursesTypeQo=new CoursesTypeQo();
			coursesTypeQo.setCoursesId(Long.parseLong(command.getTypeOne()));
			CoursesType coursesType= coursesTypeDao.queryUnique(coursesTypeQo);
			job.setTypeOne(coursesType);
		}
		if("1".equals(command.getStudent())) {
			job.setStudentType(1);
		}else{
			job.setStudentType(2);
		}
		postJobDao.save(job);
		if(command.getStudent()!=null) {
			if("1".equals(command.getStudent())) {//1全部学生、否则其它
				TeachersQo qo=new TeachersQo();
				qo.setSeachUser(true);
				qo.setSeachOrganization(true);
				qo.setTeacherId(command.getCreatUser());
				Teacher teacher= teacherDao.queryUnique(qo);
				OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
				hasStudentQo.setOrganizationId(teacher.getTraininginstitution().getId());
				List<OrganizationHasStudent> list=ostService.queryList(hasStudentQo);
				job.setStudentNum(list.size());
				for (OrganizationHasStudent organizationHasStudent : list) {
					Student student=new Student();
					student.setId(organizationHasStudent.getStudent().getId());
					JobDetails details=new JobDetails();
					details.setStudent(student);
					details.setCreaTime(new Date());
					details.setPostJob(job);
					details.setStatus(2);
					details.setUnpaidReason(0);
					jobDetailsDao.save(details);
				}

			}else {
				String[] descriptions=command.getStudent().split(",");
				job.setStudentNum(descriptions.length);
				for (String str : descriptions) {
					Student student=new Student();
					student.setId(Long.parseLong(str));
					JobDetails details=new JobDetails();
					details.setStudent(student);
					details.setCreaTime(new Date());
					details.setPostJob(job);
					details.setStatus(2);
					jobDetailsDao.save(details);
				}
			}
		}
		resultDto.setSuccess(true);
		resultDto.setResultMessage("创建成功");
		return resultDto;
	}

}
